﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Xml;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WUTOS.DataBase.DataBaseDAL;
namespace WUTOS.DataBase
{
    public partial class OptionFrame : Form
    {

        #region -变量-
        
        int LoginMode=0;
        
        int newLoginMode;
    
        //数据库版本及身份验证模式
        List<BLL.databaseversion> databaseVersion = BLL.GetLocalDataBaseVersion();

        private string eigineName = string.Empty;

        //数据库引擎名称 判定主界面服务器名称
        public string EighneName { set { eigineName = value; } get{ return eigineName; }}

        private string serverResetInfor = string.Empty;

        public string ServerResetInfor { set { serverResetInfor = value; } get { return serverResetInfor; } }
        
        //返回主界面用户模式  1 windows  2 混合 
        public int LoginModel { set { LoginMode = value; } get { return LoginMode; } }


        #endregion

        public OptionFrame()
        {
            InitializeComponent();
        }

        private void OptionFrame_Load(object sender, EventArgs e)
        {
            BindDbEngineVersion();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindDbEngineVersion()
        {
            List<string> dbVersion = new List<string>();
            dbVersion.Add("");
            foreach(BLL.databaseversion Version in databaseVersion)
            {
                dbVersion.Add(Version.version);
            }
            cbDbEngine.DataSource = dbVersion;

            BLL.databaseversion Ver = BLL.ReadConfigXml();
            for (int i = 0; i < this.cbDbEngine.Items.Count; i++)
            {
                if (Ver.version==cbDbEngine.Items[i].ToString())
                {
                    this.cbDbEngine.SelectedIndex = i;
                    break;
                }
            }
            cbDbEngine_SelectedIndexChanged(null,null);
        }

        /// <summary>
        /// 根据数据库引擎做配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbDbEngine_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.errorProvider.Clear();
            foreach (BLL.databaseversion Version in databaseVersion)
            {
                if (Version.version == cbDbEngine.Text)
                {
                    LoginMode = Version.loginmode;
                    newLoginMode = Version.loginmode;
                    rdbMixChecked.Enabled = true;
                    rdbWindowsChecked.Enabled = true;
                    //Windows模式
                    if (LoginMode == 1)
                    {
                        rdbWindowsChecked.Checked = true;
                        rdbMixChecked.Checked = false;
                    }
                    //LoginMode=2，混合模式
                    else
                    {
                        rdbMixChecked.Checked = true;
                        rdbWindowsChecked.Checked = false;
                    }
                    break;
                }
                else
                {
                    rdbWindowsChecked.Enabled = false;
                    rdbMixChecked.Enabled = false;
                }
            }

        }

        /// <summary>
        /// Windows模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdbWindowsChecked_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbWindowsChecked.Checked)
            {
                newLoginMode = 1;
            }
        }

        /// <summary>
        /// 混合模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rdbMixChecked_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbMixChecked.Checked)
            {
                newLoginMode = 2;
            }
        }

        /// <summary>
        /// 确认 切换用户模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnTrue_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Convert.ToString(this.cbDbEngine.Text.Trim())))
            {
                if (rdbMixChecked.Enabled)
                {
                    if (LoginMode != newLoginMode)
                    {
                        LoginMode = newLoginMode;
                        BLL.ChangeLoginMode(cbDbEngine.Text, newLoginMode);
                        if (MessageBox.Show("重启SQL服务后才能生效\r\n是否重启SQL服务？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                        {
                            //确定启动服务的类型：true：MSSQLSERVER，false：SQLEXPRESS
                            bool type = cbDbEngine.Text.Contains("Developer") ? true : false;
                            bool ret = BLL.RestartSqlserver(type);
                            if (!ret)
                            {
                                ServerResetInfor = "数据库配置文件丢失，重启服务失效，请联系统管理员！";
                                DialogResult = DialogResult.OK;
                            }
                            else
                            {
                                ServerResetInfor = "数据库服务器重启成功！";
                                BLL.databaseversion version = BLL.SearchLocalDbList(this.cbDbEngine.Text);
                                BLL.SaveConfigToXml(version);
                                EighneName = this.cbDbEngine.Text;
                                DialogResult = DialogResult.OK;
                            }
                        }
                        else
                        {
                            cbDbEngine_SelectedIndexChanged(sender, e);
                        }
                    }
                    else 
                    {
                        BLL.databaseversion version = BLL.SearchLocalDbList(this.cbDbEngine.Text);
                        BLL.SaveConfigToXml(version);
                        EighneName = this.cbDbEngine.Text;
                        DialogResult = DialogResult.OK;
                    }
                }
            }
            else
            {
                this.errorProvider.SetError(cbDbEngine, "数据库引擎类型为空!");
                this.ActiveControl = cbDbEngine;
            }
        }

        /// <summary>
        /// 取消 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }     
    }
}
